package com.qf.controller;

import com.qf.entity.Address;
import com.qf.entity.Cart;
import com.qf.entity.Order;
import com.qf.entity.User;
import com.qf.service.AddressService;
import com.qf.service.CartService;
import com.qf.service.OrderService;
import com.qf.service.impl.AddressServiceImpl;
import com.qf.service.impl.CartServiceImpl;
import com.qf.service.impl.OrderServiceImpl;
import com.qf.utils.CodeUtils;
import com.qf.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author 10202
 * @version V1.0
 * @Project xiaomi
 * @Package com.qf.controller
 * @Date 2023/4/14 21:15
 */
@WebServlet(urlPatterns = "/orderservlet")
public class OrderServlet extends BaseServlet{
    public String getOrderView(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            return "redirect:/login.jsp";
        }
        CartService cartService=new CartServiceImpl();
        AddressService addressService=new AddressServiceImpl();
        try {
            List<Cart> cartList = cartService.queryCartByname(user.getId());

            List<Address> addressList=addressService.queryAddressByUid(user.getId());

            request.getSession().setAttribute("cartList",cartList);
            request.getSession().setAttribute("addList",addressList);
            return "/order.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","订单页面显示异常");
            return "/message.jsp";
        }

    }
    public String addOrder(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            return "redirect:/login.jsp";
        }

        String aid = request.getParameter("aid");
        if(StringUtils.isEmpty(aid)){
            request.setAttribute("msg","地址不能为空");
            return "/message.jsp";
        }

        OrderService orderService=new OrderServiceImpl();
        CartService cartService=new CartServiceImpl();
        List<Cart> cartList = cartService.queryCartByname(user.getId());
        BigDecimal sum=new BigDecimal(0);
        for (Cart cart : cartList) {
            sum=sum.add(cart.getMoney());
        }
        Order order=new Order(CodeUtils.createOrderId(),user.getId()
                ,"1",sum,new Date(),Integer.parseInt(aid));
        try {
            orderService.addOrder(order,cartList);
            request.setAttribute("order",order);
            return "/orderSuccess.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","提交订单失败");
            return "/message.jsp";
        }


    }
}
